package com.example.demo.demotwo.stream;


import com.example.demo.model.Person;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * forEach
 *
 */
public class End {

    ArrayList<Person> People = new ArrayList<Person>();
    @Before
    public void init(){
        People = new ArrayList<Person>();
        People.add(new Person("xiaoming","22","22",1500));
        People.add(new Person("xiaohong","33","33",2500));
        People.add(new Person("xiaoli", "44", "44", 3500));
        People.add(new Person("xiaogang", "55", "55", 4500));
    }

    //collect
    @Test
    public void collectListTest(){
        List<Integer> collect = Stream.of(1, 2, 3, 4, 5, 6, 7, 8)
                .filter(num -> num % 2 == 0)
                .collect(Collectors.toList());
        System.out.println("collectListTest:");
        System.out.println(collect);
    }

    @Test
    public void findFirstTest(){
        Optional<Integer> first = Stream.of(1, 2, 3, 4, 5, 6, 7, 8)
                .sorted()
                .findFirst();
        System.out.println("findFirstTest:");
        System.out.println(first.toString());
    }

    @Test
    public void allMatchTest(){
        boolean isAllMatch = Stream.of(1, 21 , 3, 5, 7)
                .allMatch(num -> num % 2 != 0);
        System.out.println("allMatchTest:");
        System.out.println(isAllMatch);
    }

    @Test
    public void anyMatchTest(){
        boolean anyMatchTest = Stream.of(1, 21, 3, 5, 7)
                .anyMatch(num -> num % 2 == 0);
        System.out.println("anyMatchTest:");
        System.out.println(anyMatchTest);
    }

    @Test
    public void reduceTest(){
        Optional<Integer> reduce = Stream.of(1, 2, 3, 4, 5)
                .reduce((t1, t2) -> t1 > t2 ? t1 : t2);
        Integer integer = reduce.get();
        System.out.println("reduceTest");
        System.out.println(integer);
    }

    @Test
    public void reduceTest01(){
        Integer sum = Stream.of(1, 2, 3, 4)
                .reduce((t1, t2) -> t1 + t2)
                .get();
        System.out.println("reduceTest01:");
        System.out.println(sum);
    }
    
   @Test
   public void minTest(){
       Optional<Person> min = People.stream().min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
       System.out.println("minTest:");
       System.out.println(min.get());
   }

   @Test
   public void maxTest(){

       Optional<Person> max = People.stream().max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
       System.out.println("maxTest:");
       System.out.println(max.get());
    }

   @Test
   public void countTest(){
        long count = People.stream().count();
        System.out.println("countTest:");
        System.out.println(count);
    }
    
    
    















}
